global g_root  "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"

use "$g_root\Data\gfd_clean.dta", clear
merge 1:1 ym country using "$g_root\Data\gfd_clean_fwom.dta", keep(1 3) nogen

rename ret mportret

merge m:1 country using "$g_root\Data\fic_country_map.dta", keep(1 3) nogen
merge m:1 fic using "$g_root\data\fic_percentile_lag_cum_qoy.dta", keep(1 3) nogen

gen p05td = floor(p05*5/7)
gen ret_ps = .
replace ret_ps = ret0 if p05td>=5 & p05td<9
replace ret_ps = ret1 if p05td>=10 & p05td<14
replace ret_ps = ret2 if p05td>=15 & p05td<19
replace ret_ps = ret3 if p05td>=20 & !mi(p05td)

encode country, gen(country_id)
tsset country_id ym

gen ret_ps_f1m = F1.ret_ps
forvalues i = 1/12{
	gen mportret_l`i'm = L`i'.mportret
}
gen mportret_f1m = F1.mportret
gen ym_f1m = F1.ym
rename mportret mportret_l0m  
gen month = month(dofm(ym_f1m))
gen am2_flag = mod(month, 3)==1 | mod(month, 3)==2

forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	local jp3 = `j' + 3
	gen mportret_prevq2_l`i'q = mportret_l`j'm + mportret_l`jp1'm if mod(month,3)==0
	replace mportret_prevq2_l`i'q = mportret_l`jp1'm + mportret_l`jp2'm if mod(month,3)==1
	replace mportret_prevq2_l`i'q = mportret_l`jp2'm + mportret_l`jp3'm if mod(month,3)==2
}

gen mportret_prevq2_t4q = mportret_prevq2_l1q + mportret_prevq2_l2q + mportret_prevq2_l3q + mportret_prevq2_l4q 
gen mportret_prevq2_t4q_x_am2_flag = mportret_prevq2_t4q * am2_flag 
forvalues j = 1/4{
	gen mportret_prevq2_l`j'q_x_am2_flag = am2_flag * mportret_prevq2_l`j'q 
}

gen ret_s_f1m = mportret_f1m - ret_ps_f1m

* Table C3
clear matrix
local col_count = 0
local title_list	

reg mportret_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1964, 12) & am2_flag & mod(month,3)==1 & !mi(ret_ps_f1m), cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'		

reg ret_ps_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1964, 12) & am2_flag & mod(month,3)==1, cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'		
reg ret_s_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1964, 12) & am2_flag & mod(month,3)==1, cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'		

local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)
esttab sep_* using "$g_root/Output/GlobalPSOut_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 




	
